package com.hfzy.ihk.web.oauth.server.support.security;

import com.hfzy.ihk.common.web.enums.LoginErrorMsgEnum;
import com.hfzy.ihk.facade.userCenter.entity.Resource;
import com.hfzy.ihk.facade.userCenter.entity.UserHasCallLine;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.service.IUserHasCallLineService;
import com.hfzy.ihk.facade.userCenter.service.IUserService;
import com.hfzy.ihk.facade.userCenter.vo.LoginUserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wws
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    IUserService userService;

    @Value("${spring.profiles.active}")
    String profiles;

    @Autowired
    IUserHasCallLineService userHasCallLineService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserInfo user = userService.findUserByUserName(username);


        if (user == null) {
            throw new BadCredentialsException(LoginErrorMsgEnum.USER_NOT_FIND.toStr());
        }
        //查询用户按钮权限 这里包括了两条线的权限（只包括按钮权限--btnpermission）
        List<Resource> resources = userService.querybtnPermissionByUserName(username);
        user.setResources(resources);
        List<UserHasCallLine> userHasCallLines = userHasCallLineService.getUserHasCallLineByUserId(user.getId());
        user.setUserHasCallLines(userHasCallLines);
        return new LoginUserInfo(user);

    }


}


